﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Xml.Linq;
using System.Collections.ObjectModel;

namespace SLThumbsExtCF
{
    public partial class MainPage : UserControl
    {
        ClientContext ctx;
        ObservableCollection<UserControl> lstCtrl;
        public MainPage()
        {
            InitializeComponent();
            lstCtrl = new ObservableCollection<UserControl>();
            CoverFlow.ItemsSource = lstCtrl;
           
            
        }
        
        private void ReadCallback(IAsyncResult asynchronousResult)
        {
            HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
            using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
            {
                string resultString = streamReader1.ReadToEnd();
                XDocument xdoc = XDocument.Parse(resultString);

                string strImageUrl = xdoc.Element(XName.Get("mobilePres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("slideImage", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Attribute("url").Value;
                this.Dispatcher.BeginInvoke(() =>
                {
                    XElement xImageSLD = xdoc.Element(XName.Get("mobilePres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("pres", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")).Element(XName.Get("sldLst", "http://schemas.microsoft.com/server/powerpoint/2009/mobile"));
                    
                    foreach(XElement el in xImageSLD.Elements(XName.Get("sld", "http://schemas.microsoft.com/server/powerpoint/2009/mobile")))
                    {
                    string strImageID=el.Attribute("sldImg").Value;
                    string strUrl = ctx.Url + "/_layouts/" + strImageUrl + "&imageId=" + strImageID + "&width=320&height=240";
                    Uri uri = new Uri(strUrl);
                    pagePPTX page = new pagePPTX(uri);
                    page.Width = 320;
                    page.Height = 240;
                    lstCtrl.Add(page);
                    }
                });
            }
        }

        public static string strDocUrl { get; set; }
        public static string strWebUrl { get; set; }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            ctx = new ClientContext(strWebUrl);
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ctx.Url + "/_layouts/MobilePresentation.ashx?presentationId=" +  strDocUrl);
            request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
        }
    }
}
